package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @package com.itheima.service
 * @description:
 * @author: Ewaits
 * @date: 2019/7/4 22:56
 */
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> findParentMenu(List<Integer> list) {
        return menuDao.findParentMenu(list);
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = menuDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void addMainMenu(Menu menu) {
        String path = menuDao.findMaxPath();
        int Num = Integer.valueOf(path) + 1;
        menu.setPath(""+Num);
        menu.setPriority(Num);
        menuDao.addMainMenu(menu);
    }

    @Override
    public Menu findMainMenuById(Integer id) {
        return menuDao.findMainMenuById(id);
    }

    @Override
    public List<Menu> findThisChildrenMenuById(Integer id) {
        return menuDao.findThisChildrenMenuById(id);
    }

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public void deleteMainMenuById(Integer id) {
        Long count = menuDao.findCountById(id);
        Long roleCount = menuDao.findRoleCountById(id);
        if (count > 0 || roleCount > 0) {
            throw new RuntimeException("主菜单被包含");
        }
        menuDao.deleteMainMenuById(id);
    }

    @Override
    public void addChildrenMenu(Menu menu, Integer parentId) {
        Integer startPriority = menuDao.findMaxPriorityByParentId(parentId);
        int priority;
        if (startPriority == null) {
            priority=1;
        }
        String path = menuDao.findParentPath(parentId);
        priority = Integer.valueOf(startPriority) + 1;
        menu.setPath("/"+path+"-"+priority);
        menu.setPriority(priority);
        menuDao.addChildrenMenu(menu,parentId);
        setRelationWithParentMenu(menu.getId(), parentId);
    }

    private void setRelationWithParentMenu(Integer id, Integer parentId) {
        Map map = new HashMap();
        map.put("childId", id);
        map.put("parentId", parentId);
        menuDao.setRelationWithParentMenu(map);
    }

    @Override
    public Menu findMenuByMenuId(Integer id) {
        return menuDao.findMenuByMenuId(id);
    }

    @Override
    public void editMenu(Menu menu) {
        menuDao.update(menu);

    }

    @Override
    public void deleteMenuById(Integer id) {
        menuDao.setParentIdNullById(id);
        menuDao.deleteMenuById(id);
    }

    /**
     * 查询所有菜单id
     * @param list
     * @return
     */
    @Override
    public List<Integer> findMenuIds(List<Integer> list) {
        return menuDao.findMenuIds(list);
    }



}
